
const util = require("@/utils/util.js")
/**
 * pathToBase64  uni-app 插件里面图片转base64的方法
 * OcrIdCard()  判断实名信息的方法
 * access_token  百度认证的token，多数是后台从应用里面获取ak sk
 * forntOrBack  判断图片上传正面或背面
 * */
function OcrIdCard(access_token, frontOrBack) {
	return new Promise(function(resolve, reject) {
		var that = this;
		//识别身份证
		uni.chooseImage({
			count: 1,
			sizeType: ['compressed'],
			sourceType: ['album', 'camera'],
			success: function(res) {
				// console.log(res.tempFilePaths)
				let imgUrl = '';
				util.uploadFile('upload/upload', res.tempFilePaths[0]).then(function(resp) {
					imgUrl = resp.url;
					// 返回正面图片上传数据
					uni.showLoading({
						title: '识别中'
					})
					uni.request({
						url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' + access_token,
						method: 'POST',
						header: {
							'Content-Type': 'application/x-www-form-urlencoded'
						},
						data: {
							url: imgUrl,
							id_card_side: frontOrBack
						},
						success(_res) {
							uni.hideLoading();
							resolve({
								info: _res,
								img: imgUrl,
							})
						},
						fail(_res) {
							uni.hideLoading();
							uni.showToast({
								title: '请求出错',
							})
							reject(_res)
						}
					})
				})
			}
		})
	})
}

module.exports = {
	OcrIdCard: OcrIdCard
}
